package com.situ.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.util.SessionUtil;
import com.situ.crm.entity.Contract;
import com.situ.crm.entity.Student;
import com.situ.crm.entity.User;
import com.situ.crm.mapper.ContractMapper;
import com.situ.crm.mapper.StudentMapper;
import com.situ.crm.service.IStudentService;
import com.situ.crm.vo.ResultDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("studentService")
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public ResultDataVO selectStudentList(String name, String status, Integer page, Integer limit) {
        ResultDataVO vo = new ResultDataVO();
        User user = SessionUtil.getCurrentUser();
        PageHelper.startPage(page, limit);
        List<Student> list = studentMapper.selectList(name, user.getId(), status);
        vo.setCount(new PageInfo<>(list).getTotal());
        vo.setData(list);
        return vo;
    }

    @Override
    public void doSave(Student student) {
        if (student.getId() == null) {
            User user = SessionUtil.getCurrentUser();
            student.setCreateby(user.getId());
            student.setCreatetime(LocalDateTime.now());
            studentMapper.insertSelective(student);
        } else {
            studentMapper.updateByPrimaryKeySelective(student);
        }
    }

    @Override
    public Student selectStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void del(Integer id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void saveContract(Contract contract) {
        // 新增一个合同到合同表
        contractMapper.insert(contract);
        // 更新学生的状态为已签合同(1)
        studentMapper.updateStatus(contract.getStuId(), "1");
    }

    @Override
    @Transactional
    public void saveClazz(Integer clazzId, Integer stuId) {
        Student stu = new Student();
        stu.setId(stuId);
        stu.setClazzId(clazzId);
        studentMapper.updateByPrimaryKeySelective(stu);
        studentMapper.updateStatus(stuId, "2");
    }

    @Override
    public String generateInviteCode(Integer stuId) {
        List<String> list = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""));
        Collections.shuffle(list);//打乱顺序
        String code = String.join("", list.subList(0, 8));
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        operations.set("stuid-" + stuId, code,1, TimeUnit.HOURS);
        return code;
    }
}
